<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Wyp
 */

namespace App\Modules\Live\Repositories;


use App\Models\LiveAttentionMember;
use App\Modules\Live\Validators\LiveAttentionMemberValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use App\Exceptions\AllValidatorException;

class LiveAttentionMemberRepository extends BaseRepository
{
    public function model()
    {
        return LiveAttentionMember::class;
    }

    public function validator()
    {
        return LiveAttentionMemberValidator::class;
    }

    /**
     * 关注
     * @param $member_id
     * @param $user_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function createLiveAttention($member_id,$user_id)
    {
        $liveAttention = $this->getAttention($member_id,$user_id);
        if($liveAttention) {
            throw new AllValidatorException(['msg' => '您已关注过此用户']);
        }
        $data = [
            'member_id' => $member_id,
            'user_id' => $user_id,
            'status' => 1,
        ];

        return $this->create($data);
    }

    /**
     * 取消关注
     * @param $member_id
     * @param $user_id
     * @return mixed
     * @throws AllValidatorException
     */
    public function editLiveAttention($member_id,$user_id)
    {
        $liveAttention = $this->getAttention($member_id,$user_id);
        if(!$liveAttention){
            throw new AllValidatorException(['msg' => '关注不存在']);
        }
        $data = [
            'status' => 0,
        ];
        return $this->update($data,$liveAttention['id']);
    }

    /**
     * 获取关注信息
     * @param $member_id
     * @param $user_id
     * @return mixed
     */
    public function getAttention($member_id,$user_id)
    {
        $liveAttention = $this->findWhere(['member_id'=>$member_id,'user_id'=>$user_id,'status'=>1],['id','status'])->first();
        return $liveAttention;
    }

    /**
     * 获取关注数
     * @param $user_id
     * @return mixed
     */
    public function getLiveAttentionNum($user_id)
    {
        $liveAttentionNum = $this->findWhere(['user_id'=>$user_id,'status'=>1])->count();
        return $liveAttentionNum;
    }

    /**
     * 新增关注数
     * @param $user_id
     * @return mixed
     */
    public function getNewAttentionNum($user_id,$start_time)
    {
        $liveAttentionNum = $this->findWhere(['user_id'=>$user_id,'status'=>1])->where('created_at','>',$start_time)->count();
        return $liveAttentionNum;
    }
}